using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using System.Xml.XPath;
using Orchard.UI.Resources;

namespace Eking.Framework
{
    public class ResourceManifest : IResourceManifestProvider
    {
        private static Dictionary<string, string[]> _requireSets;
        public static Dictionary<string, string[]> RequireSets
        {
            get
            {
                //if (_requireSets == null)
                //{
                var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Modules", "Eking.Framework", "ResourceManifestSettings.xml");
                var ele = XElement.Load(path);
                _requireSets = ele.XPathSelectElements("//Set").ToDictionary(e => e.Attribute("Name").Value,
                                                                      e =>
                                                                      new[] { e.Attribute("Styles").Value, e.Attribute("Scripts").Value });

                //}

                return _requireSets;

            }
        }

        private static dynamic GetValue(XElement e)
        {
            return new
                       {
                           Name = e.Attribute("Name").Value,
                           Url = e.Attribute("Url") == null
                                     ? e.Attribute("Name").Value
                                     : e.Attribute("Url").Value,
                           IsFolder = e.Attribute("IsFolder") == null
                                          ? false
                                          : bool.Parse(e.Attribute("IsFolder").Value.ToLower()),
                           Tree = e.Attribute("Tree") == null
                                      ? false
                                      : bool.Parse(e.Attribute("Tree").Value.ToLower()),
                           Folder = e.Attribute("Folder") == null
                                     ? null
                                     : e.Attribute("Folder").Value
                       };
        }

        private static dynamic GetRes(string file, string root)
        {
            return new { Name = Path.GetFileName(file), Url = "/" + file.Replace(AppDomain.CurrentDomain.BaseDirectory, "") };
        }
        public void BuildManifests(ResourceManifestBuilder builder)
        {
            try
            {
                var manifest = builder.Add();
                var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Modules", "Eking.Framework", "ResourceManifestSettings.xml");
                var ele = XElement.Load(path);

                var defines = ele.XPathSelectElements("//Style").Select(GetValue).ToList();
                var realdefines = defines.Where(d => !d.IsFolder).ToList();

                defines.Where(d => d.IsFolder).ToList().ForEach(d =>
                {
                    var fo = (string)Path.Combine(AppDomain.CurrentDomain.BaseDirectory, d.Folder);
                    var vals = Directory.EnumerateFiles(fo, "*.css", d.Tree ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly).Select(f => GetRes(f, fo));
                    realdefines.AddRange(vals);
                });

                realdefines.ForEach(s => manifest.DefineStyle(s.Name).SetUrl(s.Url));

                defines = ele.XPathSelectElements("//Script").Select(GetValue).ToList();
                realdefines = defines.Where(d => !d.IsFolder).ToList();
                defines.Where(d => d.IsFolder).ToList().ForEach(d =>
                {
                    var fo = (string)Path.Combine(AppDomain.CurrentDomain.BaseDirectory, d.Folder);
                    var vals = Directory.EnumerateFiles(fo, "*.js", d.Tree ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly).Select(f => GetRes(f, fo));
                    realdefines.AddRange(vals);
                });


                realdefines.ForEach(s => manifest.DefineScript(s.Name).SetUrl(s.Url));

                //manifest.DefineScript("jquery.mousewheel.js").SetUrl("jquery.mousewheel.js").SetDependencies("jquery.jscrollpane.min.js");
            }
            catch (Exception ex)
            {

                throw;
            }
        }
    }
}
